
# $Id: Volume.pm 149 2013-04-13 05:42:04Z chengxin.cai $

use strict;

package SQConfig::Volume;

use Switch;
use Carp;

use PerlUtil qw( gen_getter gen_setter );

use lib '/opt/hp/nv/lib/perl';
use instance ':all';

sub import {
    my $package = shift;
    $package =~ s/.*:://;

    ##export the constructor
    no strict 'refs';
    my ($caller) = caller;
    *{ $caller . '::' . $package } = \&{$package};

    gen_getter( __PACKAGE__, 'name' );
    gen_getter( __PACKAGE__, 'nodeid' );
    gen_getter( __PACKAGE__, 'prim_loc' );
    gen_getter( __PACKAGE__, 'mirr_loc' );
    gen_getter( __PACKAGE__, 'cabno' );
    gen_getter( __PACKAGE__, 'as_string' );

    gen_setter( __PACKAGE__, 'set_cabno' );
}

sub Volume {
    my ($line) = @_;

    my @fields = split /\s+/, $line;
    my $name     = $fields[0];
    my $nodeid   = $fields[1];
    my $prim_loc = $fields[2];
    my $mirr_loc = $fields[3];
    my $cabno;

    bless sub {
        my $op = shift;

        switch ($op) {
            case 'name'      { return $name; }
            case 'nodeid'    { return $nodeid; }
            case 'cabno'     { return $cabno; }
            case 'prim_loc'  { return $prim_loc; }
            case 'mirr_loc'  { return $mirr_loc; }
            case 'set_cabno' { return sub { $cabno = shift; } }
            case 'as_string' { return sprintf "%-6s\t\t%s\t\t%s\t\t\t%s",
                                              $name, $nodeid,
                                              $prim_loc, $mirr_loc;
                             }
            else             { croak "Invalid operation $op"; }
        }
    }, __PACKAGE__;
}

use overload q{""} => \&as_string;

1;
